package com.example.qixinshangwutong.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixinshangwutong.R;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PwdLoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_login);

        initView();
    }

    public void initView(){

        Button loginByPwdBtn = findViewById(R.id.login_btn_by_pwd);

        EditText phoneText = findViewById(R.id.login_username_text_by_pwd);

        EditText pwdText = findViewById(R.id.login_pwd_text_by_pwd);

        ImageButton imageButton = findViewById(R.id.login_go_back_btn_by_pwd);

        TextView btnUnKnowPwd = findViewById(R.id.un_know_pwd_btn);

        loginByPwdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phone = phoneText.getText().toString();

                String pwd = pwdText.getText().toString();

                LCUser.logIn(phone,pwd)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCUser>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCUser lcUser) {
                                Toast.makeText(PwdLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(PwdLoginActivity.this,BottomActivity.class);
                                startActivity(intent);
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {
                                Toast.makeText(PwdLoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });

            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PwdLoginActivity.this,BottomActivity.class);

                startActivity(intent);
            }
        });

        btnUnKnowPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(PwdLoginActivity.this,ModifyActivity.class);

                startActivity(intent);

            }
        });
    }

}